package com.xs.bookspoststation.db;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xs.bookspoststation.bean.User;

import java.util.ArrayList;
import java.util.List;

public class UserBean {
    private Context context;
    private DBHelperUser dbHelperUser;
    public UserBean(Context context){
        this.context=context;
        dbHelperUser=new DBHelperUser(context,"books_station.db",null,1);
    }

    @SuppressLint("Range")
    public List<User> findAll(){
        List<User> users = null;
        SQLiteDatabase db = dbHelperUser.getWritableDatabase();
        if (db.isOpen()) {
            Cursor cursor = db.query("book_user", null, null, null, null, null, null);
            users = new ArrayList<User>();
            while (cursor.moveToNext()) {
                User user = new User();
                //Xxx getXxx(columnIndex):根据字段下标得到对应的值
                //int getColumnIndex():根据字段名得到对应的下标
                //cursor.getLong()：以 long 形式返回所请求列的值。
                //getColumnIndex() 获取给定列名的从零开始的列索引,如果列名不存在返回-1
                String username = cursor.getString(cursor.getColumnIndex("username"));
                user.setUsername(username);
                String userpwd = cursor.getString(cursor.getColumnIndex("userpwd"));
                user.setUserpwd(userpwd);
                String userphone = cursor.getString(cursor.getColumnIndex("userphone"));
                user.setUserphone(userphone);
                String usersex = cursor.getString(cursor.getColumnIndex("usersex"));
                user.setUsersex(usersex);
                String userhobby = cursor.getString(cursor.getColumnIndex("userhobby"));
                user.setUserhobby(userhobby);
                String usereducation = cursor.getString(cursor.getColumnIndex("usereducation"));
                user.setUsereducation(usereducation);
                String userfullname = cursor.getString(cursor.getColumnIndex("userfullname"));
                user.setUserfullname(userfullname);
                String useremail = cursor.getString(cursor.getColumnIndex("useremail"));
                user.setUseremail(useremail);
                String userprovince = cursor.getString(cursor.getColumnIndex("userprovince"));
                user.setUserprovince(userprovince);
                String userschool = cursor.getString(cursor.getColumnIndex("userschool"));
                user.setUserschool(userschool);
                users.add(user);
            }
            cursor.close();
            db.close();
        }
        return users;
    }
}
